package cn.ac.iie.di.ban.ycl.cleaner.handler;

import cn.ac.iie.di.datadock.rdata.exchange.client.v1.connection.REConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.logging.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.Signal;
import sun.misc.SignalHandler;

/**
 * @author zmc <zhangmingcheng@iie.ac.cn>
 * @date 2016-9-7 14:32:27
 * @version V1.0
 * @desc
 */
public class KillHandler implements SignalHandler {

    public static Logger LOG = LoggerFactory.getLogger(KillHandler.class.getName());
    private REConnection conn;
//    private ExecutorService pool;
    private Map<String, BlockingQueue> queues = new HashMap<>();

    public void registerSignal(String signalName) {
        Signal signal = new Signal(signalName);
        Signal.handle(signal, this);
    }

    @Override
    public void handle(Signal signal) {
        LOG.info("now receive the system signal " + signal.getName() + " " + signal.getNumber() + "\n\n");
        if (signal.getName().equals("TERM")) {
            try {
                conn.close();
                LOG.info("shutdown mq connection ...");
//                pool.shutdown();
//                LOG.info("shutdown thread pool ...");
                checkQueues();
                LOG.info("shutdown send queue...");
            } catch (Exception ex) {
                LOG.error("Program shudown error!", ex);
            } finally {
                LOG.info("Program has shutdown !");
                System.exit(0);
            }
        } else if (signal.getName().equals("INT") || signal.getName().equals("HUP")) {
            System.exit(0);
        } else {
            LOG.info("can not process the system signal " + signal.toString());
        }
    }

    public void checkQueues() {
        while (true) {
            boolean isEmpty = true;
            for (Entry<String, BlockingQueue> queue : queues.entrySet()) {
                String queueName = queue.getKey();
                BlockingQueue queueObj = queue.getValue();
                int size = queueObj.size();
                if (size != 0) {
                    isEmpty = false;
                    LOG.info("the {} has {} datas!", queueName, size);
                    break;
                }
            }
            if (isEmpty) {
                break;
            } else {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                }
            }
        }

    }

    public void setConn(REConnection conn) {
        this.conn = conn;
    }

//    public void setPool(ExecutorService pool) {
//        this.pool = pool;
//    }

    public void putQueue(String queueName, BlockingQueue queue) {
        this.queues.put(queueName, queue);
    }
}
